<template>
	<view class="upbox">
		<view class="setlist">
			<view class="nrws u-f u-f-ac u-f-jsb">
				<view class="u-f1">投放单价(最低{{zprice}})<text>*</text></view>
				<input class="u-f3" type="number" v-model="price" :min="zprice"  placeholder="请填写单价"  />
			</view>
			<view class="nrws u-f u-f-ac u-f-jsb">
				<view class="u-f1">投放数量 <text>*</text></view>
				<input class="u-f3" type="number" v-model="sum"  placeholder="最低10个"  />
			</view>
			<view class="nrwss u-f u-f-ac u-f-jsb">
				<view class="u-f1">结束时间 <text  class="ssie">*</text></view>
				<view class="u-f3">
					<picker mode="date" :value="date" :start="startDate" :end="endDate" @change="bindDateChange">
						<view>{{date}}<text class="cell-more yticon icon-you"></text></view>
					</picker>
				</view>
			</view>
			<view class="nrwss u-f u-f-ac u-f-jsb">
				<view class="u-f1">提交限时 <text class="ssie">*</text></view>
				<view class="u-f3">
					<picker @change="bindPickerChange" :value="index" :range="array" range-key="name">
						<view>{{array[index].name}}<text class="cell-more yticon icon-you"></text></view>
					</picker>
					
				</view>
			</view>
			<view class="nrwss u-f u-f-ac u-f-jsb">
				<view class="u-f1">审核周期 <text  class="ssie">*</text></view>
				<view class="u-f3"><picker @change="bindPickerChangezj" :value="zjindex" :range="zjay" range-key="name">
						<view>{{zjay[zjindex].name}}<text class="cell-more yticon icon-you"></text></view>
					</picker></view>
			</view>
			<view class="nrwss u-f u-f-ac u-f-jsb">
				<view class="u-f1">支付方式 <text  class="ssie">*</text></view>
				<view class="u-f3"><picker @change="bindPickerChangepay" :value="zjindex" :range="payarr" range-key="name">
						<view>{{payarr[payindex].name}}<text class="cell-more yticon icon-you"></text></view>
					</picker></view>
			</view>
		</view>
		<view class="upOnbox u-f u-f-ac u-f-jsb">
			<view class="yuise u-f2">总计：<text class="sungs">￥{{price*sum}}</text></view>
			<view class="upOnTaps u-f1" @tap="okTap()" >提交订单</view>
		</view>
	</view>
</template>

<script>
	function getDate(type) {
		const date = new Date();
	
		let year = date.getFullYear();
		let month = date.getMonth() + 1;
		let day = date.getDate();
	
		if (type === 'start') {
			year = year - 60;
		} else if (type === 'end') {
			year = year + 2;
		}
		month = month > 9 ? month : '0' + month;;
		day = day > 9 ? day : '0' + day;
	
		return `${year}-${month}-${day}`;
	}
export default {
		data() {
			return {
				title: 'picker',
				array: [{name:'请选择'},{name:'2小时'},{name: '3小时'}],
				index: 0,
			    zjay: [{name:'请选择'},{name:'24小时'},{name: '48小时'},{name: '72小时'}],
				zjindex: 0,
				payarr: [{name:'微信'},{name: '余额'}],
				payindex: 0,
				multiIndex: [0, 0, 0],
				date: '数量完成自动结束',
				startDate:getDate('start'),
				endDate:getDate('end'),
				price:'',
				sum:'',
				dataList:[],
				zprice:0,
			}
		},	
		onLoad(e) {
			this.dataList=uni.getStorageSync("rwdata")
			console.log(this.dataList);
			console.log('起来')
			var that=this;
			uni.request({
				url: that.$serverUrl + '/index.php/Mobile/index/getzuidi',
				success: res =>{
					console.log('asdad 阿三大苏打')
					if(res.data.status==200){
					
					this.zprice=res.data.zprice
					}else{
					
					}
				},
				fail: (data, code) => {
					//console.log('fail' + JSON.stringify(data));
				}
			});
		},
		methods: {
			bindPickerChange: function(e) {
				console.log('picker发送选择改变，携带值为：' + e.detail.value)
				this.index = e.detail.value
			},
			bindPickerChangezj: function(e) {
				console.log('picker发送选择改变，携带值为：' + e.detail.value)
				this.zjindex = e.detail.value
			},
			bindDateChange: function(e) {
				this.date = e.detail.value
			},
			bindPickerChangepay: function(e) {
				console.log('picker发送选择改变，携带值为：' + e.detail.value)
				this.payindex = e.detail.value
			},
			okTap(){
				var that=this
				var thia=this;
				uni.showLoading({
					title: '支付中'
				});
				if(!this.price){
					this.$api.msg('投放单价不能为空');
					return;
				}
				if(this.price<this.zprice){
				this.$api.msg('不能低于最低价格');
				return;	
				}
				if(!this.sum){
					this.$api.msg('投放数量不能为空');
					return;
				}
				if(this.array[this.index].name=='请选择'){
					this.$api.msg('提交时间不能为空');
					return;
				}
				if(this.zjay[this.zjindex].name=='请选择'){
					this.$api.msg('审核周期不能为空');
					return;
				}
				if(this.payarr[this.payindex].name=='请选择支付方式'){
					this.$api.msg('请选择支付方式');
					return;
				}
				this.dataList.price=this.price;
				this.dataList.sum=this.sum;
				this.dataList.endtime=this.date,
				this.dataList.subtime=this.array[this.index].name;
				this.dataList.shetime=this.zjay[this.zjindex].name;
				this.dataList.buytype=this.payarr[this.payindex].name;
				//https://rw.gzzsw.cn/api/pay/index?token=a69720a4-6c4c-4baf-b269-d5260b821c7a
				let data = this.dataList;
				data.token = uni.getStorageSync("userInfo").token;
				data.uid = uni.getStorageSync("userInfo").id;
				console.log(data.buz)
				data.buzs = JSON.stringify(data.buz); 
				console.log(data)
				uni.request({
					url: that.$serverUrl + '/index.php/Mobile/index/addrenwu',
					data: data,
					success: resa =>{
						console.log(resa.data);
						console.log(thia.dataList.buytype)
						if(thia.dataList.buytype=='微信'){
						//this.$api.msg(resa.data.msg);
						var pay=resa.data;
						console.log(pay.appid);
						console.log('进入微信支付')
						uni.requestPayment({
												provider: 'wxpay',
												"orderInfo": {
						                        "appid":pay.appid ,  // 微信开放平台 - 应用 - AppId，注意和微信小程序、公众号 AppId 可能不一致
						                        "noncestr": pay.noncestr, // 随机字符串
						                        "package": "Sign=WXPay",        // 固定值！！！！
						                        "partnerid":pay.partnerid,      // 微信支付商户号
						                        "prepayid": pay.prepayid, // 统一下单订单号 
						                        "timestamp": pay.timestamp,        // 时间戳（单位：秒）
						                        "sign": pay.sign // 签名，这里用的 MD5/RSA 签名
						                        },
													success: e=>{
														uni.hideLoading();
														console.log("支付成功");
														uni.reLaunch({
														url:"/pages/index/index"	
														})
													}, 
													fail: err=>{
														if(err.errMsg.indexOf('取消') > -1 || err.errMsg.indexOf('cancel') >1 || err.errMsg.indexOf('-2') > -1){
															console.log('取消支付');
														}else{
															console.log('支付遇到错误，请稍候重试');
															console.log(err);
														}
													}
												})
						}else if(thia.dataList.buytype=='余额'){
							if(resa.data.code==1){
								//pages/user/task/task
								uni.showModal({
									title: '提示',
									content: resa.data.msg,
									showCancel: false,
									success: res => {
										uni.navigateBack({
											delta:2,
										})
									}
								});
							}
						}
						//thia.okPay(resa)
					},
					fail: (data, code) => {
						console.log('fail' + JSON.stringify(data));
					}
				});
				
				
			},
			okPay(orderInfo){
				console.log(orderInfo);
				uni.requestPayment({
				    provider: 'alipay',
				    orderInfo: orderInfo.data, //微信、支付宝订单数据
				    success: function (res) {
						uni.showModal({
							title: '提示',
							content: '发布成功',
							showCancel: false,
							success: res => {
								uni.navigateBack({
									delta:2,
								})
							}
						});
				        //console.log('success:' + JSON.stringify(res));
				    },
				    fail: function (err) {
						this.$api.msg(JSON.stringify(err));
				        //console.log('fail:' + JSON.stringify(err));
				    }
				});
			}
		}	
}		
</script>

<style>
	page{
		background: #f3f3f3;
	}
	.yuise{
		text-align: center;
	}
	.upbox{
		background: #fff;
	}
	.sungs{
		color: #FF4443;
		font-size: 36upx;
		font-weight: 500;
	}
	.setlist{
		width: 94%;
		margin: 0 auto;
	}
	.nrws{
		padding: 26upx 0;
		border-bottom: 2upx solid #E4E7ED;
	}
	.nrws input{
		font-size: 30upx;
		text-align: right;
	}
	.nrws view:nth-child(1){
		font-size: 32upx;
		color: #111;
		width: 80px;
	}
	.nrws view:nth-child(2){
		text-align: right;
	}
	.nrws view:nth-child(1) text{
		color: #FF3333;
	}
	.setlist .nrws:last-child{
		border-bottom: none;
	}
	
	.nrwss{
		padding: 26upx 0;
		border-bottom: 2upx solid #E4E7ED;
	}
	.nrwss view:nth-child(1){
		font-size: 32upx;
		color: #111;
	}
	.nrwss view:nth-child(2){
		text-align: right;
	}
	.ssie{
		color: #FF3333;
	}
	.upOnbox{
		width: 100%;
		background: #fff;
		position: fixed;
		left: 0;
		right: 0;
		bottom: 0;
		padding: 20upx 40upx;
		box-sizing: border-box;
	}
	.upOnTaps{
		width: 60%;
		margin: 0 auto;
		height: 80upx;
		line-height: 80upx;
		text-align: center;
		background: #FFB400;
		color: #000;
		border-radius: 60px;
		font-size: 32upx;
	}
</style>
